package com.dhcc.DRGsHosp.application.impl;

import com.dhcc.DRGsHosp.application.MrAppendPageApplication;
import com.dhcc.DRGsHosp.core.domain.MrAppendPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Named;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 首页附页表--数据访问层--实体类
 * @ClassName: MrAppendPageApplicationImpl  
 * @Description ：    首页附页表
 * @Date: 2018年7月30日 下午1:54:08  
 * @Author QiaoYiLong 
 * @Version 3.1.1 DHC-DRG
 */
@Named("drgsHospMrAppendPageApplicationImpl20181212")
@Transactional
public class MrAppendPageApplicationImpl implements MrAppendPageApplication {

	@Autowired
	private NamedParameterJdbcTemplate jdbcTempSeverice;

	/**
	 *  取得主索引为pmainIndex的 首页附页表信息
	 *  @Author QiaoYiLong
	 *  @param pmainIndex   主索引
	 *  @Date:2018年7月30日上午11:46:45
	 */
	@Override
	public MrAppendPage getMrAppendPage(String pmainIndex) {
		return MrAppendPage.get(MrAppendPage.class, pmainIndex);
	}
	/**
	 *  创建首页附页表
	 *  @Author QiaoYiLong
	 *  @Date:2018年7月30日上午11:48:34
	 */
	@Override
	public void createMrAppendPage(MrAppendPage mrAppendPage) {
		mrAppendPage.save();
	}
	/**
	 *  修改首页附页表
	 *  @Author QiaoYiLong
	 *  @Date:2018年7月30日上午11:48:56
	 */
	@Override
	public void updateMrAppendPage(MrAppendPage mrAppendPage) {
		mrAppendPage.save();
	}
	/**
	 *  删除一个首页附页表
	 *  @Author QiaoYiLong
	 *  @Date:2018年7月30日上午11:49:20
	 */
	@Override
	public void removeMrAppendPage(MrAppendPage mrAppendPage) {
		if( mrAppendPage != null ){
			mrAppendPage.remove();
		}
	}
	/**
	 *  删除多个首页附页表
	 *  @Author QiaoYiLong
	 *  @Date:2018年7月30日上午11:49:43
	 */
	@Override
	public void removeMrAppendPages(Set<MrAppendPage> mrAppendPages) {
		for( MrAppendPage mrAppendPage : mrAppendPages){
			mrAppendPage.remove();
		}
	}
	/**
	 *  查询所有的首页附页表信息
	 *  @Author QiaoYiLong
	 *  @Date:2018年7月30日上午11:50:03
	 */
	@Override
	public List<MrAppendPage> findAllMrAppendPage() {
		return MrAppendPage.findAll(MrAppendPage.class);
	}

	/**
	 * @param condition
	 * @return
	 * @throws
	 * @Description: 查询病例附页信息
	 * @author chenhanlin
	 * @date 2018/7/29 15:51
	 */
	@Override
	public List<MrAppendPage> queryMrAppendPageByCondition(Map<String, Object> condition) {
		StringBuffer sqlb = new StringBuffer() ;
		sqlb.append(" SELECT a.pmainindex \"pmainIndex\",\n" ) ;
		sqlb.append(" P90010000  \"p90010000\",\n" ) ;
		sqlb.append(" P90010001  \"p90010001\",\n" ) ;
		sqlb.append(" P90020000  \"p90020000\",\n" ) ;
		sqlb.append(" P90020001  \"p90020001\",\n" ) ;
		sqlb.append(" P90031000  \"p90031000\",\n" ) ;
		sqlb.append(" P90031001  \"p90031001\",\n" ) ;
		sqlb.append(" P90032000  \"p90032000\",\n" ) ;
		sqlb.append(" P90041000  \"p90041000\",\n" ) ;
		sqlb.append(" P90041001  \"p90041001\",\n" ) ;
		sqlb.append(" P90042000  \"p90042000\",\n" ) ;
		sqlb.append(" P90051000  \"p90051000\",\n" ) ;
		sqlb.append(" P90051001  \"p90051001\",\n" ) ;
		sqlb.append(" P90052000  \"p90052000\",\n" ) ;
		sqlb.append(" P90061000  \"p90061000\",\n" ) ;
		sqlb.append(" P90061001  \"p90061001\",\n" ) ;
		sqlb.append(" P90062000  \"p90062000\",\n" ) ;
		sqlb.append(" P90070000  \"p90070000\",\n" ) ;
		sqlb.append(" P90080000  \"p90080000\",\n" ) ;
		sqlb.append(" P90080001  \"p90080001\",\n" ) ;
		sqlb.append(" P90090000  \"p90090000\",\n" ) ;
		sqlb.append(" P90090001  \"p90090001\",\n" ) ;
		sqlb.append(" P90100000  \"p90100000\",\n" ) ;
		sqlb.append(" P90100001  \"p90100001\",\n" ) ;
		sqlb.append(" P90110000  \"p90110000\",\n" ) ;
		sqlb.append(" P90110001  \"p90110001\",\n" ) ;
		sqlb.append(" P90120000  \"p90120000\",\n" ) ;
		sqlb.append(" P90120001  \"p90120001\",\n" ) ;
		sqlb.append(" P90130000  \"p90130000\",\n" ) ;
		sqlb.append(" P90130001  \"p90130001\",\n" ) ;
		sqlb.append(" P90140000  \"p90140000\",\n" ) ;
		sqlb.append(" P90140001  \"p90140001\",\n" ) ;
		sqlb.append(" P90150000  \"p90150000\",\n" ) ;
		sqlb.append(" P90150001  \"p90150001\",\n" ) ;
		sqlb.append(" P90120201  \"p90120201\",\n" ) ;
		sqlb.append(" P90160000  \"p90160000\",\n" ) ;
		//下面是（中铁十七局特有病案首页）
		sqlb.append(" P90200001  \"p90200001\",\n" ) ;
		sqlb.append(" P90210001  \"p90210001\",\n" ) ;
		sqlb.append(" P90215001  \"p90215001\",\n" ) ;
		sqlb.append(" P90220001  \"p90220001\",\n" ) ;
		sqlb.append(" P90230001  \"p90230001\",\n" ) ;
		sqlb.append(" P90240001  \"p90240001\",\n" ) ;
		sqlb.append(" P90250001  \"p90250001\",\n" ) ;
		sqlb.append(" P90260001  \"p90260001\",\n" ) ;
		sqlb.append(" P90270001  \"p90270001\",\n" ) ;
		sqlb.append(" P90280001  \"p90280001\",\n" ) ;
		sqlb.append(" P90290001  \"p90290001\",\n" ) ;
		sqlb.append(" P00475000  \"p00475000\",\n" ) ;
		sqlb.append(" P00475001  \"p00475001\",\n" ) ;
		sqlb.append(" P00335000  \"p00335000\",\n" ) ;
		sqlb.append(" P00336000  \"p00336000\"\n" ) ;
		//（中铁十七局特有病案首页）结束
		sqlb.append(" FROM drgs.MR_APPENDPAGE app \n" ) ;
		sqlb.append(" left join drgs.mr_baseinfo a ON a.PMAININDEX = app.PMAININDEX left join drgs.MR_IPINFO ipinfo ON app.PMAININDEX = ipinfo.PMAININDEX \n" ) ;
		sqlb.append(" WHERE 1=1 \n" ) ;
		String sql = sqlb.toString() ;
		// 时间条件
		if(condition.get("dateFrom")!=null && condition.get("dateTo") !=null){
			String dateField = "" ;
			// 时间字段
			if("outDate".equals(condition.get("dateType"))){ // 出院日期
				dateField = "ipinfo.P00290000" ;
			} else if("pushDate".equals(condition.get("dateType"))) { // 推送日期
				dateField = "a.push_date" ;
			}
			sql = sql.replace("1=1", " 1=1 and to_char("+dateField+",'yyyy-mm-dd')  BETWEEN :dateFrom AND :dateTo ") ;
		}

		if(condition.get("hospCode") != null){
			sql = sql.replace("1=1" ," 1=1 and instr(:hospCode,a.p00010001)>0 ") ;
		}

		// 分页sql
		String pageSqlHead = "SELECT * FROM (SELECT PAGERESULT.*, ROWNUM RN FROM (";
		String pageSqlFoot = " ORDER BY ipinfo.PMAININDEX desc) PAGERESULT ) WHERE rn>(:pageNo-1)*:pageSize and rn <=:pageNo*:pageSize ";

		List<MrAppendPage> mrAppendPageList = jdbcTempSeverice.query(pageSqlHead+sql+pageSqlFoot,condition , new BeanPropertyRowMapper<MrAppendPage>(MrAppendPage.class));
		return mrAppendPageList;
	}

}
